package com.china08.yxyapi.api.manage.sch.platform.website.image;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.db.sws.SwsSettingSlidersForApp;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSettingSlidersForAppRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController("ImageForAppUpdController")
public class ImageForAppUpdController {

    @Autowired
    SwsSettingSlidersForAppRepository swsSettingSlidersRepository;

    @ApiOperation("修改(微网站)焦点图")
    @RequestMapping(value = "/api_yw/manage/sch/platform/website/imageforapp/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public void precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String id = reqModel.id;
        String img = reqModel.img;
        String url = reqModel.url;

        SwsSettingSlidersForApp swsSettingSliders = swsSettingSlidersRepository.findById(id);
        Assert.isTrue(swsSettingSliders != null, "未获取当前记录");

        swsSettingSliders.setUrl(url);
        swsSettingSliders.setImg(img);
        swsSettingSlidersRepository.save(swsSettingSliders);
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String id;

        // 图片地址
        public String img;

        // 链接地址
        public String url;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(id), "焦点图为空");
            Assert.isTrue(StringUtils.isNotBlank(img), "焦点图不能为空");
            if (StringUtils.isNotBlank(url)) {
                boolean b1 = url.startsWith("http://");
                boolean b2 = url.startsWith("https://");
                Assert.isTrue(b1 || b2, "链接地址必须以http://或https://开头");
            }
        }
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
